package com.jhhc.Weathers.datasupplement.util;


import cn.hutool.http.HttpUtil;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jhhc.Weathers.datasupplement.entity.BdmsWfProductF;
import com.jhhc.Weathers.datasupplement.entity.jcdzBO.Base;
import com.jhhc.Weathers.datasupplement.entity.jcdzBO.Login;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取数据工具类
 * @author LYQ
 */

public class GettingData {

    /**
     * 通过接口下载数据
     */
    public List<BdmsWfProductF> downloadData(String token , String liuyu, String dataType,
                                             String ybTime,
                                             int provideid, String qbTime){
        System.out.println("获取数据");
        String url = "http://58.59.29.50:15005/api/getBasinGridDataByNameAndTime";
        Map<String,Object> map = new HashMap<>();
        map.put("basinName",liuyu);
        map.put("dataType",dataType);
        map.put("ybTime",ybTime);
        map.put("token",token);
        map.put("qbTime",qbTime);
        String result = HttpUtil.get(url,map);
        JSONArray jsonArray = JSONArray.parseArray(result);
        if (jsonArray != null && jsonArray.size() > 0) {
            //json字符串转为json数组
            //JSONObject jsonObject = JSONObject.parseObject(result);
            //结果为对象
            JSONObject object = jsonArray.getJSONObject(0);
            //获取对象中的data
            JSONArray data = object.getJSONArray("data");
            //获取对象中的base
            JSONObject base = object.getJSONObject("base");
            Base b = JSON.to(Base.class,base);
            System.out.println("数据获取完成");
            List<BdmsWfProductF> bdmsWfProductFS = new ArrayList<>();
            BdmsWfProductF bdmsWfProductF = null;
            int grid = 0;
            double y = Double.parseDouble(b.getMinLat());
            for (int i = 0; i < data.size(); i++) {
                double x = Double.parseDouble(b.getMinLon());
                for (int j = 0; j < data.getJSONArray(i).size(); j++) {
                    bdmsWfProductF = new BdmsWfProductF();
                    bdmsWfProductF.setProvideid(provideid);
                    bdmsWfProductF.setRtm(StructureTime.timeConstruction(qbTime));
                    bdmsWfProductF.setDtlen(Integer.valueOf(b.getEndTimeIndex()));
                    bdmsWfProductF.setIntv(24);
                    bdmsWfProductF.setGridX(String.format("%.6f",x));
                    bdmsWfProductF.setGridY(String.format("%.6f",y));
                    bdmsWfProductF.setGridP(data.getJSONArray(i).get(j).toString());
                    bdmsWfProductF.setGridId(grid);
                    bdmsWfProductF.setGridnm("China5km");
                    bdmsWfProductFS.add(bdmsWfProductF);
                    x += 0.05;
                    grid++;
                }
                y += 0.05;
            }
            System.out.println("入库对象构造完成");
            return bdmsWfProductFS;
        }else {
            return null;
        }

    }

    /**
     * 通过接口获取token
     */
    public static String getToken(){
        String url = "http://58.59.29.50:15005/auth/login";
        Login login = new Login();
        login.setUsername("hwj629");
        login.setPassword("hwj@2023");
        String param = JSONObject.toJSONString(login);
        String result = HttpUtil.post(url,param);
        JSONObject jsonObject = JSONObject.parseObject(result);
        return (String) jsonObject.get("token");
    }

}
